#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QPalette pal = palette();
    pal.setBrush(QPalette::Base, Qt::transparent);
    setPalette(pal);
    setAttribute(Qt::WA_TranslucentBackground, true);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::init()
{
    vi_1=Link::create("InputVi");
    QVariantMap dataVi_1;
    dataVi_1["interface"]=Link::getConfig()["interfaceV"].toMap().firstKey();
    vi_1->start(dataVi_1);

    if(Link::getConfig()["interfaceV"].toMap().keys().count()>1)
    {
        //如果有第二个视频输入口就用
        vi_2=Link::create("InputVi");
        QVariantMap dataVi_2;
        dataVi_2["interface"]=Link::getConfig()["interfaceV"].toMap().keys()[1];
        vi_2->start(dataVi_2);
    }
    else
    {
        //否则，将第一路视频裁剪放大，充当第二路视频
        vi_2=Link::create("Crop");
        QVariantMap crop;
        crop["x"]=0.25;
        crop["y"]=0.25;
        crop["width"]=0.5;
        crop["height"]=0.5;
        crop["pass"]=false;
        vi_2->start(crop);
        vi_1->linkV(vi_2);
    }


    mixV=Link::create("MixV");
    QVariantMap dataMixV;
    QVariantList srcs;
    QVariantList layouts;
    QVariantMap lay;
    lay["x"]=0;
    lay["y"]=0;
    lay["w"]=0.6;
    lay["h"]=0.6;
    lay["a"]=1;
    lay["index"]=0;
    layouts<<lay;
    lay["x"]=0.4;
    lay["y"]=0.4;
    lay["index"]=1;
    layouts<<lay;
    dataMixV["layout"]=layouts;
    srcs<<vi_1->name()<<vi_2->name();
    dataMixV["src"]=srcs;
    mixV->start(dataMixV);

    vi_1->linkV(mixV);
    vi_2->linkV(mixV)->linkV(vo);

}

void Widget::on_pushButton_clicked()
{
    QVariantList list;
    list<<vi_1->name()<<vi_2->name();
    mixV->invoke("swap",list);
}
